{include file='default/head' /}
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space1">
        <div class="layui-col-xs12 layui-col-sm12 layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    {$title}{$uid!=null?'（商户UID：'.$uid.'）':''}{$out_trade_no!=null?'（系统订单号：'.$out_trade_no.'）':''}
                </div>
                <div class="layui-card-body">

                    <!--                    -->
                    <div class="layui-form search">
                        <div class="layui-inline">
                            <select name="search_type" id="search_type">
                                <option value="name">交易名称</option>
                                <option value="out_trade_no" {$out_trade_no!=null?'selected':''}>系统订单号</option>
                                <option value="api_trade_no">接口订单号</option>
                                <option value="money">支付金额</option>
                            </select>
                        </div>
                        <div class="layui-inline">
                            <input class="layui-input" id="search_value" autocomplete="off"
                                   placeholder="留空查询则重载全部" value="{$search_value!=null?$search_value:''}">
                        </div>
                        <div class="layui-inline">
                            <select class="form-control" id="search_pay_type">
                                <option value="all">全部支付方式</option>
                                {foreach name="pay_list" item="v" key="key"}
                                <option value="{$v.alias}">{$v.name}</option>
                                {/foreach}
                            </select>
                        </div>
                        <div class="layui-inline">
                            <input class="layui-input" id="search_uid" autocomplete="off"
                                   placeholder="商户UID - 留空则全部商户" value="{$uid!=null?$uid:''}">
                        </div>
                        <div class="layui-inline">
                            <select class="form-control" id="search_state">
                                <option value="all">全部订单状态</option>
                                {foreach name="state" item="v" key="key"}
                                <option value="{$key}">{$v}</option>
                                {/foreach}
                            </select>
                        </div>
                        <button class="layui-btn" data-type="reload" id="btn_search"><i
                                class="layui-icon layui-icon-search"></i> 查询
                        </button>
                    </div>

                    <script type="text/html" id="data_Table_toolbar">
                        <div class="layui-btn-container">
                            <button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="del">删除选中订单</button>
                        </div>
                    </script>

                    <!-- 表格 -->
                    <table id="data_Table" lay-filter="data_Table"></table>
                    <!-- 表格 -->

                    <script type="text/html" id="oper">
                        <a class="layui-btn layui-btn-xs" lay-event="info">详情</a>
                        {{#  if(d.refund_money>0){ }}
                        {{#  }else{ }}
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="refund">退款</a>
                        {{#  } }}
                        <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="del">删除</a>
                    </script>

                    <script type="text/html" id="type_temp">
                        <img style="vertical-align:middle" src="__PUBLIC__/icon/{{ d.type_icon }}" width="17"> <b>{{
                            d.type }}</b>
                    </script>
                    <script type="text/html" id="state_temp">
                        {{#  if(d.state == "0"){ }}
                        <span style="color: #f00"><b>
                        {{#  }else{ }}
                       <span style="color: #00f"><b>
                        {{#  } }}
                        {{ d.state_name }}</b></span>
                    </script>
                    <script type="text/html" id="payment_time">
                        {{#  if(d.payment_time != "" && d.payment_time != null){ }}
                        {{ d.payment_time }}
                        {{#  }else{ }}
                        <span style="color: #f00"><b>无</b></span>
                        {{#  } }}
                    </script>
                    <script type="text/html" id="refund_time">
                        {{#  if(d.refund_money <= 0){ }}
                        <span style="color: #f00"><b>未退款</b></span>
                        {{#  }else{ }}
                        {{ d.refund_time }}
                        {{#  } }}
                    </script>
                    <script type="text/html" id="out_trade_no">
                        {{#  if(d.out_trade_no != "" && d.out_trade_no != null){ }}
                        {{ d.out_trade_no }}
                        {{#  }else{ }}
                        无
                        {{#  } }}
                    </script>
                    <script type="text/html" id="api_trade_no">
                        {{#  if(d.api_trade_no != "" && d.api_trade_no != null){ }}
                        {{ d.api_trade_no }}
                        {{#  }else{ }}
                        <span style="color: #f00"><b>无</b></span>
                        {{#  } }}
                    </script>
                    <script type="text/html" id="third_trade_no">
                        {{#  if(d.third_trade_no != "" && d.third_trade_no != null){ }}
                        {{ d.third_trade_no }}
                        {{#  }else{ }}
                        <span style="color: #f00"><b>无</b></span>
                        {{#  } }}
                    </script>
                    <script type="text/html" id="look_remarks">
                        {{#  if(d.remarks != "" && d.remarks != null){ }}
                        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="look_remarks">查看</a>
                        {{#  }else{ }}
                        无
                        {{#  } }}
                    </script>
                    <script type="text/html" id="source">
                        {{ d.source_name }}&nbsp;&nbsp;
                    {{#  if(d.source=='api' && d.state!='0'){ }}
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="notify">通知</a>
                    {{#  } }}
                    </script>

                </div>
            </div>
        </div>
    </div>
</div>

<style id="welcome_style"></style>
<div class="layui-col-md12">
    <blockquote class="layui-elem-quote layui-quote-nm">{$core['ADMIN_BOTTOM']}</blockquote>
</div>
<script>
    layui.use(['jquery', 'form', 'layedit', 'laydate', 'element', 'laytpl', 'table', 'laypage', 'layer', 'carousel', 'slider'], function () {
        $ = layui.jquery;
        var form = layui.form //表单
            , layer = layui.layer  //弹出层
            , laypage = layui.laypage //分页
            , layedit = layui.layedit  //元素操作
            , laydate = layui.laydate  //日期
            , laytpl = layui.laytpl  //模板引擎
            , table = layui.table  //表格
            , element = layui.element;
//===================数据初始化
        table.render({
            elem: '#data_Table'
            , height: 450
            , url: '/admin/Bill/getList'
            , method: "POST"
            , page: true //开启分页
            , even: true //开启隔行背景
            , toolbar: '#data_Table_toolbar'
            , title: "表格数据"
            , cols: [[ //表头
                {type: 'checkbox', fixed: "left"}
                , {field: 'id', title: 'ID', width: 70, sort: true}
                , {field: 'source', title: '订单来源', width: 140, toolbar: '#source'}
                , {field: 'trade_type', title: '支付场景', width: 120}
                , {field: 'out_trade_no', title: '系统订单号', width: 200, templet: "#out_trade_no"}
                , {field: 'api_trade_no', title: '接口订单号', width: 200, templet: "#api_trade_no"}
                , {field: 'third_trade_no', title: '第三方订单号', width: 200, templet: "#third_trade_no"}
                , {field: 'uid', title: '商户UID', width: 100}
                , {field: 'type', title: '支付方式', width: 100, templet: "#type_temp"}
                , {field: 'state', title: '订单状态', width: 120, templet: "#state_temp"}
                , {field: 'pid', title: '接口名称', width: 120}
                , {field: 'money', title: '支付金额', width: 100}
                , {field: 'name', title: '交易名称', width: 150}
                , {field: 'creation_time', title: '创建时间', width: 155}
                , {field: 'payment_time', title: '支付时间', width: 155, templet: "#payment_time"}
                , {field: 'refund_time', title: '退款时间', width: 155, templet: "#refund_time"}
                , {field: 'remarks', title: '备注', width: 80, align: 'center', toolbar: '#look_remarks'}
                , {
                    field: 'oper',
                    title: '操作',
                    width: 190,
                    unresize: true,
                    fixed: "right",
                    align: 'center',
                    toolbar: '#oper'
                }
            ]]
            , even: true
            , id: 'data_Table'
            , limit: 15 //每页默认显示的数量
            , limits: []//每页条数的选择项
            , text: {
                none: '暂无数据'
            }
        });

        var $ = layui.$, active = {
            reload: function () {
                var search_type = $('#search_type').val();  //搜索类型
                var search_value = $('#search_value').val();  //搜索内容
                var search_uid = $('#search_uid').val();  //商户UID
                var search_pay_type = $('#search_pay_type').val();  //支付方式
                var search_state = $('#search_state').val();  //订单状态
                layer.msg('拼命查询中...', {icon: 16, shade: 0.3, time: 1500});
                //执行重载
                table.reload('data_Table', {
                    page: {
                        curr: 1 //从第 1 页开始
                    }
                    , method: "POST"
                    //额外接口参数
                    , where: {
                        mode: search_type
                        , value: search_value
                        , uid: search_uid
                        , type: search_pay_type
                        , state: search_state
                    }
                });
            }
        };
        $("#btn_search").click(function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        })

        {if condition = "$uid != null or $search_value !=null"}
        $('#btn_search').trigger("click");
        {/if}

            //头工具栏事件
            table.on('toolbar(data_Table)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                if (data.length <= 0) {
                    layer.msg("至少选中一条");
                    return;
                }
                let ids = [];
                data.forEach((item) => {
                    ids.push(item.id);
                });
                switch (obj.event) {
                    case 'del':
                        var index1 = layer.confirm('你确定要将所选中的【删除】吗？（' + (checkStatus.isAll ? '已全选' : '选中了' + data.length + ' 条') + '）<br/><span style="color: #f00">1.删除后将无法恢复！！！<br/>2.删除后交易额将会产生影响。<br/><b>请慎重操作，操作后无法撤销！</b></span>', {
                            btn: ['确认删除', '算了'] //按钮
                        }, function () {
                            layer.close(index1);
                            var index2 = layer.prompt(
                                {
                                    title: '请输入（6位）安全密码'
                                    , formType: 1
                                    , maxlength: 6
                                },
                                function (aqmm) {
                                    layer.close(index2);
                                    oper("del", ids.join(","), aqmm);
                                });
                        });
                        break;
                    default:
                        layer.msg("不支持该操作类型");
                }
                ;
            });

            //操作
            table.on('tool(data_Table)', function (obj) {
                var data = obj.data;
                var event = obj.event;
                switch (event) {
                    case "del":
                        var index1 = layer.confirm('你确定要删除该交易记录吗？<br/><span style="color: #f00">1.删除后将无法恢复！！！<br/>2.删除后交易额将会产生影响。<br/><b>请慎重操作！</b></span>', {
                            btn: ['确认删除', '算了'] //按钮
                        }, function () {
                            layer.close(index1);
                            var index2 = layer.prompt(
                                {
                                    title: '请输入（6位）安全密码'
                                    , formType: 1
                                    , maxlength: 6
                                },
                                function (aqmm) {
                                    layer.close(index2);
                                    oper(event, data.id, aqmm);
                                });
                        });
                        break;
                    case "refund":
                        var index1 = layer.confirm('你确定要申请退款吗？（申请后无法撤回）<br/><span style="color: #f00">全 额 退 款：<b>' + data.money + '元</b><br/>系统订单号：<b>' + data.out_trade_no + '</b><br/>接口订单号：<b>' + data.api_trade_no + '</b></span>', {
                            btn: ['确认退款', '算了'] //按钮
                        }, function () {
                            layer.close(index1);
                            var index2 = layer.prompt(
                                {
                                    title: '请输入（6位）安全密码'
                                    , formType: 1
                                    , maxlength: 6
                                },
                                function (aqmm) {
                                    layer.close(index2);
                                    var Load = layer.load(2, {shade: [0.3, '#000']}); //开始加载
                                    $.ajax({
                                        type: "POST",
                                        url: "/refund",
                                        data: {
                                            "way": "admin",
                                            "out_trade_no": data.out_trade_no,
                                            "security_password": aqmm
                                        },
                                        dataType: "json",
                                        success: function (json) { //SUCCESS
                                            var data = $.parseJSON(json);
                                            if (data.code == 0) {
                                                layer.close(Load); //关闭加载
                                                var indexa = layer.msg(data.msg, {time: 1000, icon: 6}, function () {
                                                    layer.close(indexa);
                                                    //执行重载
                                                    table.reload('data_Table', {
                                                        page: {
                                                            curr: 1 //从第 1 页开始
                                                        }
                                                        , method: "POST"
                                                        //额外接口参数
                                                        , where: {
                                                            mode: "all"
                                                        }
                                                    });
                                                    return;
                                                });
                                            } else {
                                                layer.close(Load); //关闭加载
                                                var indexa = layer.alert(data.msg, {
                                                    title: data.title,
                                                    icon: 5
                                                }, function () {
                                                    layer.close(indexa);
                                                    if (data.url != "" && data.url != null) {
                                                        window.location.href = data.url;
                                                    }
                                                })
                                            }
                                        },
                                        error: function () { //ERROR
                                            layer.close(Load); //关闭加载
                                            layer.msg('系统繁忙');
                                            return false;
                                        }
                                    });
                                });
                        });
                        break;
                    case "look_remarks":
                        layer.alert(data.remarks);
                        break;
                    case "info":
                        layer.open({
                            type: 1,
                            scrollbar: false,
                            title: "订单详情",
                            shadeClose: true,
                            shade: 0.3,
                            area: [layerWidth, "85%"],
                            content: "<div style=\"padding: 10px 8px\">\n" +
                                "                        <blockquote class=\"layui-elem-quote layui-quote-nm\" style=\"padding: 0px 10px\">\n" +
                                "\n" +
                                "                            <table class=\"layui-table\">\n" +
                                "                                <colgroup>\n" +
                                "                                    <col width=\"100\">\n" +
                                "                                    <col>\n" +
                                "                                </colgroup>\n" +
                                "                                <tbody>\n" +
                                "                                <tr>\n" +
                                "                                    <td>ID</td>\n" +
                                "                                    <td>" + data.id + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>订单来源</td>\n" +
                                "                                    <td>" + data.source_name + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>商户UID</td>\n" +
                                "                                    <td>" + data.uid + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>交易名称</td>\n" +
                                "                                    <td>" + data.name + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>支付接口</td>\n" +
                                "                                    <td>" + data.pid + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>支付方式</td>\n" +
                                "                                    <td><img style=\"vertical-align:middle\" src=\"__PUBLIC__/icon/" + data.type_icon + "\" width=\"15\"> <b>" + data.type + "</b>【" + data.trade_type + "】</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>支付金额</td>\n" +
                                "                                    <td>￥" + data.money + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>系统订单号</td>\n" +
                                "                                    <td>" + data.out_trade_no + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>接口订单号</td>\n" +
                                "                                    <td>" + data.api_trade_no + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>第三方订单号</td>\n" +
                                "                                    <td>" + data.third_trade_no + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>创建时间</td>\n" +
                                "                                    <td>" + data.creation_time + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>支付时间</td>\n" +
                                "                                    <td>" + data.payment_time + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>过期时间</td>\n" +
                                "                                    <td>" + data.expiration_time + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>交易关闭时间</td>\n" +
                                "                                    <td>" + data.close_time + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>交易状态</td>\n" +
                                "                                    <td>" + data.state_name + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>IP地址</td>\n" +
                                "                                    <td>" + data.ip + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>买家标识</td>\n" +
                                "                                    <td>" + data.buyer + "</td>\n" +
                                "                                </tr>\n" +
                                "                                <tr>\n" +
                                "                                    <td>交易备注</td>\n" +
                                "                                    <td>" + data.remarks + "</td>\n" +
                                "                                </tr>\n" +
                                "                                </tbody>\n" +
                                "                            </table>\n" +
                                "\n" +
                                "                        </blockquote>\n" +
                                "</div>"
                        });
                        break;
                    case "notify":
                        var Load = layer.msg('等待第三方响应异步通知...', {icon: 16, shade: 0.3}); //开始加载
                        $.ajax({
                            type: "POST",
                            url: "/user/bill/again_notify_single",
                            data: {"out_trade_no":data.out_trade_no},
                            dataType: "json",
                            success: function (json) { //SUCCESS
                                var data = $.parseJSON(json);
                                if (data.code == 0) {
                                    layer.close(Load); //关闭加载
                                    var indexa = layer.msg(data.msg, {time: 1000, icon: 6}, function () {
                                        layer.close(indexa);
                                    });
                                } else {
                                    layer.close(Load); //关闭加载
                                    var indexa = layer.alert(data.msg, {title: data.title, icon: 5}, function () {
                                        layer.close(indexa);
                                        if (data.url != "" && data.url != null) {
                                            window.location.href = data.url;
                                        }
                                    })
                                }
                            },
                            error: function () { //ERROR
                                layer.close(Load); //关闭加载
                                layer.msg('系统繁忙');
                                return false;
                            }
                        });
                        break;
                    default:
                        layer.alert("不支持该操作类型")
                }
            });


            function oper(mode, id, aqmm = "") {
                var Load = layer.load(2, {shade: [0.3, '#000']}); //开始加载
                $.ajax({
                    type: "POST",
                    url: "/admin/bill/oper",
                    data: {"mode": mode, "id": id, "security_password": aqmm},
                    dataType: "json",
                    success: function (json) { //SUCCESS
                        var data = $.parseJSON(json);
                        if (data.code == 0) {
                            layer.close(Load); //关闭加载
                            var indexa = layer.msg(data.msg, {time: 1000, icon: 6}, function () {
                                layer.close(indexa);
                                //执行重载
                                table.reload('data_Table', {
                                    page: {
                                        curr: 1 //从第 1 页开始
                                    }
                                    , method: "POST"
                                    //额外接口参数
                                    , where: {
                                        mode: "all"
                                    }
                                });
                            });
                        } else {
                            layer.close(Load); //关闭加载
                            var indexa = layer.alert(data.msg, {title: data.title, icon: 5}, function () {
                                layer.close(indexa);
                                return false;
                            })
                        }
                    },
                    error: function () { //ERROR
                        layer.close(Load); //关闭加载
                        layer.msg('系统繁忙');
                        return false;
                    }
                });
            }
        }
    );
</script>
</body>
</html>